package com.uha.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uha.reggie.common.ReggieResult;
import com.uha.reggie.entity.Category;
import com.uha.reggie.entity.Employee;
import com.uha.reggie.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author uha
 * @date 2023/6/15
 * @apiNote
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增分类
     * @param category category
     * @return ReggieResult
     */
    @PostMapping
    public ReggieResult<String> sort(@RequestBody Category category){

        categoryService.save(category);

        return ReggieResult.success("添加分类成功~");

    }

    /**
     * MP 分页
     * @param page page
     * @param pageSize pageSize
     //* @param name name
     * @return
     */
    @GetMapping("/page")
    public ReggieResult<Page> page(int page, int pageSize){

        //构造分页构造器
        Page<Category> pageInfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.orderByAsc(Category::getSort);

        categoryService.page(pageInfo,queryWrapper);
        return ReggieResult.success(pageInfo);

    }

    /**
     * 删除分类
     */
    @DeleteMapping
    public ReggieResult<String> delete( Long ids ){


        categoryService.remove(ids);

        return ReggieResult.success("category");
    }

    /**
     * 修改分类
     */
    @PutMapping
    public ReggieResult<String> edit(@RequestBody Category category){

        categoryService.updateById(category);

        return ReggieResult.success("成功修改分类信息~");
    }

    /**
     * 根据条件查询分类数据
     *
     */

    @GetMapping("/list")
    public ReggieResult<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());

        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(queryWrapper);

        return ReggieResult.success(list);
    }




}
